﻿using AutoMapper;
using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command;

namespace U.B.WriteApi.CommandHander
{
    public class UpdateLeaveCommandHander : IRequestHandler<UpdateLeaveCommand, ApiResult<string>>
    {
        private readonly IRepistory<LeaveApp> leaveRep;
        private readonly IRepistory<Approval> approvalRep;
        private readonly IMapper mapper;

        public UpdateLeaveCommandHander(IRepistory<LeaveApp> leaveRep, IRepistory<Approval> approvalRep, IMapper mapper)
        {
            this.leaveRep = leaveRep;
            this.approvalRep = approvalRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<string>> Handle(UpdateLeaveCommand request, CancellationToken cancellationToken)
        {
            ApiResult<string> result = new ApiResult<string>();

            //查询当前申请表
            var leave = leaveRep.GetAll().FirstOrDefault(x => x.LAId == request.LAId);
            //查询领导人的人数 LeaderCount
            var LeaderCount = leave.LeaderIds.Split(',').Length;
            //查询已审批人数 AppCount
            var AppCount = approvalRep.GetAll().Where(x => x.LAId == request.LAId).ToList().Count;

            //无人审批 AppCount==0||AppCount<LeaderCount-1 通过 审批中 不通过 审批中  
            if (AppCount == 0 || AppCount < LeaderCount - 1)
            {

                var m = mapper.Map<LeaveApp>(leave);
                m.StatusId = (int)Status.审批中;
                m.StatusName = "审批中";
                leaveRep.Update(m);



            }

            //最后一人审批 通过 已审批 不通过 已拒绝

            if (AppCount == LeaderCount - 1)
            {
                if (request.AppResult == (int)AppResult.通过)
                {
                    var m = mapper.Map<LeaveApp>(leave);
                    m.StatusId = (int)Status.已审批;
                    m.StatusName = "已审批";
                    leaveRep.Update(m);

                }
                else
                {
                    var m = mapper.Map<LeaveApp>(leave);
                    m.StatusId = (int)Status.已拒绝;
                    m.StatusName = "已拒绝";
                    leaveRep.Update(m);

                }

            }

            //所有人审批且通过 通过已通过 不通过已拒绝
            var AppCounts = approvalRep.GetAll().Where(x => x.LAId == request.LAId && x.AppResult == (int)AppResult.通过).ToList().Count;
            if (AppCount == LeaderCount && AppCounts == LeaderCount)
            {
                if (request.AppResult == (int)AppResult.通过)
                {
                    var m = mapper.Map<LeaveApp>(leave);
                    m.StatusId = (int)Status.终审_已通过;
                    m.StatusName = "终审_已通过";
                    leaveRep.Update(m);

                }
                else
                {
                    var m = mapper.Map<LeaveApp>(leave);
                    m.StatusId = (int)Status.终审_已拒绝;
                    m.StatusName = "终审_已拒绝";
                    leaveRep.Update(m);

                }

            }
            //审批记录添加
            Approval approval = new Approval();
            approval.LAId = request.LAId;
            approval.LeaderId = request.UserId;
            approval.LeaderName = request.UserName;
            approval.AppResult = request.AppResult;
            approval.Reason = request.Reason;
            approval.CreateTime = DateTime.Now;
            approvalRep.Add(approval);

            result.code = Renum.审批成功;
            result.Message = "审批成功";
            return Task.FromResult(result);
        }
    }
}
